package ejb;

import java.util.ArrayList;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import repositories.AirportRepository;
import repositories.CityRepository;
import repositories.FlightRepository;
import specifications.AirportSpecification;
import specifications.CitySpecification;
import specifications.FlightSpecification;
import entities.Airport;
import entities.City;
import entities.Flight;

/**
 * Session Bean implementation class FlightRepository
 */
@Stateless
@LocalBean
public class FlightRepositoryBean implements FlightRepositoryBeanLocal {

    /**
     * Default constructor. 
     */
    public FlightRepositoryBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public ArrayList<Flight> getAllFlights() {
		FlightRepository FR = new FlightRepository();
		FR.FindAll();
		return FR.FlightList;
	}

	@Override
	public ArrayList<Flight> getSpecifiedFlights(String from, String where,
			String when) {
		CityRepository CRep = new CityRepository();
		FlightRepository FRep = new FlightRepository();
		AirportRepository ARep = new AirportRepository();
		CRep.FindAll();
		FRep.FindAll();
		ARep.FindAll();
		
		CitySpecification citySpecDep = new CitySpecification("cityName",
				from);
		ArrayList<City> cityDep = CRep.findBySpecification(citySpecDep);
		
		CitySpecification citySpecArr = new CitySpecification("cityName",
				where);
		ArrayList<City> cityArr = CRep.findBySpecification(citySpecArr);
		
		AirportSpecification airportSpecDep = new AirportSpecification(
				"airportCity", cityDep.get(0).getCityName());
		ArrayList<Airport> AirportsDep = ARep
				.findBySpecification(airportSpecDep);
		
		AirportSpecification airportSpecArr = new AirportSpecification(
				"airportCity", cityArr.get(0).getCityName());
		ArrayList<Airport> AirportsArr = ARep
				.findBySpecification(airportSpecArr);
		
		
		ArrayList<Flight> resFlDep = new ArrayList<Flight>();
		for (int i = 0; i < AirportsDep.size(); i++) {
			FlightSpecification flSpecDep = new FlightSpecification(
					"departureAirport", AirportsDep.get(i).getAirportName());
			ArrayList<Flight> flights = FRep.findBySpecification(flSpecDep);
			resFlDep.addAll(flights);
		}
		ArrayList<Flight> resFl = new ArrayList<Flight>();
		for(int i = 0 ; i<AirportsArr.size();i++)
		{
			FlightSpecification flSpecArr = new FlightSpecification(
					"arriveAirport", AirportsArr.get(i).getAirportName());
			ArrayList<Flight> flights = FRep.findBySpecification(flSpecArr,resFlDep);
			resFl.addAll(flights);
		}
		ArrayList<Flight> flights = new ArrayList<Flight>();
		for(int i=0; i<resFl.size();i++)
		{
			FlightSpecification flSpecArr = new FlightSpecification(
					"departureDateTime",when);
			 flights = FRep.findBySpecification(flSpecArr,resFl);
		}
		return flights;
	}

	@Override
	public ArrayList<Flight> getSpecifiedFlights(FlightSpecification flSpec) {
		FlightRepository FR = new FlightRepository();
		FR.FindAll();
		
		return FR.findBySpecification(flSpec);
	}

	@Override
	public ArrayList<Flight> getSpecifiedFlights(FlightSpecification flSpec,
			ArrayList<Flight> flightList) {
		FlightRepository FR = new FlightRepository();
		
		return FR.findBySpecification(flSpec, flightList);
	}

	@Override
	public ArrayList<Flight> getSpecifiedFlightsbyAirports(
			String when, String fromAirport, String whereAirport) {
		FlightRepository FRep = new FlightRepository();
		AirportRepository ARep = new AirportRepository();
		FRep.FindAll();
		ARep.FindAll();
		
		AirportSpecification airportSpecDep = new AirportSpecification(
				"airportName", fromAirport);
		ArrayList<Airport> AirportsDep = ARep
				.findBySpecification(airportSpecDep);
		
		AirportSpecification airportSpecArr = new AirportSpecification(
				"airportName", whereAirport);
		ArrayList<Airport> AirportsArr = ARep
				.findBySpecification(airportSpecArr);
		
		
		ArrayList<Flight> resFlDep = new ArrayList<Flight>();
			FlightSpecification flSpecDep = new FlightSpecification(
					"departureAirport", AirportsDep.get(0).getAirportName());
			resFlDep = FRep.findBySpecification(flSpecDep);
			FlightSpecification flSpecArr = new FlightSpecification(
					"arriveAirport", AirportsArr.get(0).getAirportName());
		
		ArrayList<Flight> resFl = FRep.findBySpecification(flSpecArr,resFlDep);
		FlightSpecification flSpecWhen = new FlightSpecification(
				"departureDateTime",when);
		resFl = FRep.findBySpecification(flSpecWhen,resFl);
		

		return resFl;
	}

	@Override
	public void updateFlight(Flight flight) {
		FlightRepository FRep = new FlightRepository();
		FRep.Update(flight);
		
	}

}
